package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.vo.ProTaskVo;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.ProTaskTempBo;
import com.ruoyi.system.domain.vo.ProTaskTempVo;
import com.ruoyi.system.domain.ProTaskTemp;
import com.ruoyi.system.mapper.ProTaskTempMapper;
import com.ruoyi.system.service.IProTaskTempService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 任务管理模板Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-20
 */
@RequiredArgsConstructor
@Service
public class ProTaskTempServiceImpl implements IProTaskTempService {

    private final ProTaskTempMapper baseMapper;

    private final ISysUserService sysUserService;

    /**
     * 查询任务管理模板
     */
    @Override
    public ProTaskTempVo queryById(Long taskId){
        ProTaskTempVo proTaskTempVo = baseMapper.selectVoById(taskId);
        proTaskTempVo.setHeadUsers(this.parseUsersBynames(proTaskTempVo.getHeadUser()));
        proTaskTempVo.setCopyUsers(this.parseUsersBynames(proTaskTempVo.getCopyUser()));
        return proTaskTempVo;
    }
    private ArrayList<SysUser> parseUsersBynames(String headUser){
        ArrayList<SysUser> headUsers = new ArrayList<>();
        if(StringUtils.isNotBlank(headUser)){
            String[] split = headUser.split(",");
            for (String username : split) {
                SysUser sysUser = sysUserService.selectUserByUserName(username);
                if(ObjectUtils.isNotNull(sysUser)){
                    headUsers.add(sysUser);
                }
            }
        }
        return headUsers;
    }
    /**
     * 查询任务管理模板列表
     */
    @Override
    public TableDataInfo<ProTaskTempVo> queryPageList(ProTaskTempBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProTaskTemp> lqw = buildQueryWrapper(bo);
        Page<ProTaskTempVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(item->{
            item.setHeadUsers(this.parseUsersBynames(item.getHeadUser()));
            item.setCopyUsers(this.parseUsersBynames(item.getCopyUser()));
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务管理模板列表
     */
    @Override
    public List<ProTaskTempVo> queryList(ProTaskTempBo bo) {
        LambdaQueryWrapper<ProTaskTemp> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProTaskTemp> buildQueryWrapper(ProTaskTempBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProTaskTemp> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTaskId() != null, ProTaskTemp::getTaskId, bo.getTaskId());
        lqw.eq(bo.getDdate() != null, ProTaskTemp::getDdate, bo.getDdate());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), ProTaskTemp::getTaskName, bo.getTaskName());
        lqw.eq(StringUtils.isNotBlank(bo.getDesp()), ProTaskTemp::getDesp, bo.getDesp());
        lqw.eq(bo.getFireDate() != null, ProTaskTemp::getFireDate, bo.getFireDate());
        lqw.eq(bo.getDemandDate() != null, ProTaskTemp::getDemandDate, bo.getDemandDate());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ProTaskTemp::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadUser()), ProTaskTemp::getHeadUser, bo.getHeadUser());
        lqw.eq(StringUtils.isNotBlank(bo.getCopyUser()), ProTaskTemp::getCopyUser, bo.getCopyUser());
        lqw.eq(bo.getSubjectId() != null, ProTaskTemp::getSubjectId, bo.getSubjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectNo()), ProTaskTemp::getSubjectNo, bo.getSubjectNo());
        lqw.like(StringUtils.isNotBlank(bo.getSubjectName()), ProTaskTemp::getSubjectName, bo.getSubjectName());
        lqw.eq(bo.getRate() != null, ProTaskTemp::getRate, bo.getRate());
        return lqw;
    }

    /**
     * 新增任务管理模板
     */
    @Override
    public Boolean insertByBo(ProTaskTempBo bo) {
        ProTaskTemp add = BeanUtil.toBean(bo, ProTaskTemp.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTaskId(add.getTaskId());
        }
        return flag;
    }

    /**
     * 修改任务管理模板
     */
    @Override
    public Boolean updateByBo(ProTaskTempBo bo) {
        ProTaskTemp update = BeanUtil.toBean(bo, ProTaskTemp.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProTaskTemp entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除任务管理模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
